/*
ont-space - The ontology-based resource metadata repository
Copyright (c) 2006-2008, Information Eng. Research Unit - Univ. of Alcalá
http://www.cc.uah.es/ie
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.ontspace.util.translator.lom2owl.owl;

import org.ieee.ltsc.lom.LOM;
import org.ieee.ltsc.lom.LOMUtil;
import org.ieee.ltsc.datatype.Entity;
import org.ieee.ltsc.datatype.DateTime;

import org.ietf.mimedir.vcard.*;

import java.io.*;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.RDFNode;

/**
 * 
 * Translates a LOM annotation element to its representation in OWL. 
 */
public class A2ATranslator extends ElementTranslatorImpl {

    private String category =
            new String();
    private String attributeName =
            new String();
    private String element =
            new String();

    private enum CAMPOS {

        FN, N, EMAIL, ORG, OTRO
    }

    /** Creates a new instance of A2ATranslator */
    public A2ATranslator(String elementCategory, String elementElement,
            String elementAttribute) {

        this.category =
                elementCategory;
        this.attributeName =
                elementAttribute;
        this.element =
                elementElement;
    }

    /**     
     * This method translates a LOM annotation element to its representation 
     * in OWL. 
     * @param lom The metadata record
     * @param ont The ontology
     * @param inst The instance of learning Object
     * @param nsName The namespace
     * @param errorWriter The error file
     */
    public void apply(LOM lom, OntModel ont, Individual inst, String nsName,
            PrintWriter errorWriter) {

        OntClass lomAnnotationClass =
                ont.getOntClass(nsName + "#lomAnnotation");
        OntClass VCardClass =
                ont.getOntClass(nsName + "#vCard");
        OntClass singleLangStringClass =
                ont.getOntClass(nsName +
                "#singleLangString");
        OntClass classDateTime =
                ont.getOntClass(nsName + "#lomDateTime");
        OntClass langStringClass =
                ont.getOntClass(nsName + "#langString");
        DatatypeProperty emailProperty =
                ont.getDatatypeProperty(nsName +
                "#hasEmail");
        DatatypeProperty nameProperty =
                ont.getDatatypeProperty(nsName +
                "#hasName");
        DatatypeProperty fNameProperty =
                ont.getDatatypeProperty(nsName +
                "#hasFName");
        DatatypeProperty organizationProperty =
                ont.getDatatypeProperty(nsName +
                "#hasOrganization");
        DatatypeProperty hasValueProperty =
                ont.getDatatypeProperty(nsName +
                "#hasValue");
        DatatypeProperty inLanguageProperty =
                ont.getDatatypeProperty(nsName +
                "#inLanguage");
        DatatypeProperty dateTimeProperty =
                ont.getDatatypeProperty(nsName +
                "#hasDateTimeValue");
        ObjectProperty includeLangStringProperty =
                ont.getObjectProperty(nsName + "#includesSingleLangString");
        ObjectProperty annotationDescriptionProperty =
                ont.getObjectProperty(nsName + "#annotationDescription");
        ObjectProperty annotationProperty =
                ont.getObjectProperty(nsName +
                "#hasAnnotation");

        String instance =
                null;
        int i =
                0;

        while (LOMUtil.getAnnotation(lom, i) != null) {

            Individual lomAnnotationInstance =
                    ont.createIndividual(nsName +
                    "#Annotation" + i, lomAnnotationClass);

            //8.1 entity         
            Entity ent =
                    LOMUtil.getAnnotationEntity(lom, i);
            instance =
                    "#vCardA" +
                    inst.getLocalName() + "-" + String.valueOf(i);
            Individual VCardInstance =
                    ont.createIndividual(nsName + instance,
                    VCardClass);
            VCard MimeDirImpl2 =
                    ent.vcard();

            CAMPOS campo =
                    null;

            int lines =
                    MimeDirImpl2.getContentLineCount();

            for (int contador =
                    0; i < lines; i++) {

                if (MimeDirImpl2.getContentLine(contador).getValueType().
                        toString().contains("N:")) {
                    campo =
                            CAMPOS.N;
                } else if (MimeDirImpl2.getContentLine(contador).getValueType().
                        toString().contains("FN:")) {
                    campo =
                            CAMPOS.FN;
                } else if (MimeDirImpl2.getContentLine(contador).getValueType().
                        toString().contains("EMAIL:")) {
                    campo =
                            CAMPOS.EMAIL;
                } else if (MimeDirImpl2.getContentLine(contador).getValueType().
                        toString().contains("ORG:")) {
                    campo =
                            CAMPOS.ORG;
                } else {
                    campo =
                            CAMPOS.OTRO;
                }

                switch (campo) {
                    case N:  //Name

                        try {
                            String nameLOM =
                                    MimeDirImpl2.getContentLine(contador).
                                    getValueType().toString();
                            if (nameLOM != null) {
                                RDFNode nameValue =
                                        ont.createTypedLiteral(nameLOM);
                                VCardInstance.setPropertyValue(nameProperty,
                                        nameValue);
                            }
                        } catch (Exception e) {
                            String log =
                                    "The name field of the Annotation category must contain a value";
                            errorWriter.println(log + '\n' + e);
                        }

                        break;

                    case FN:  //FName

                        try {
                            String fNameLOM =
                                    MimeDirImpl2.getContentLine(contador).
                                    getValueType().toString();
                            if (fNameLOM != null) {
                                RDFNode fNameValue =
                                        ont.createTypedLiteral(fNameLOM);
                                VCardInstance.setPropertyValue(fNameProperty,
                                        fNameValue);
                            }
                        } catch (Exception e) {
                            String log =
                                    "The name field of the Annotation category must contain a value";
                            errorWriter.println(log + '\n' + e);
                        }
                        break;


                    case EMAIL:  //Email


                        try {
                            String emailLOM =
                                    MimeDirImpl2.getContentLine(contador).
                                    getValueType().toString();
                            if (emailLOM != null) {
                                RDFNode emailValue =
                                        ont.createTypedLiteral(emailLOM);
                                VCardInstance.setPropertyValue(emailProperty,
                                        emailValue);
                            }
                        } catch (Exception e) {
                            String log =
                                    "The Email field of the Annotation category must contain a value";
                            errorWriter.println(log + '\n' + e);
                        }

                        break;

                    case ORG:  //Organization

                        try {
                            String organizationLOM =
                                    MimeDirImpl2.getContentLine(contador).
                                    getValueType().toString();
                            if (organizationLOM != null) {
                                RDFNode organizationValue =
                                        ont.createTypedLiteral(organizationLOM);
                                VCardInstance.setPropertyValue(organizationProperty,
                                        organizationValue);
                            }
                        } catch (Exception e) {
                            String log =
                                    "The Organization field of the Annotation category must contain a value";
                            errorWriter.println(log + '\n' + e);
                        }
                        break;

                    case OTRO:
                        break;


                }
            }


            //8.2 date time

            DateTime dat =
                    LOMUtil.getAnnotationDate(lom, i);
            instance =
                    "#dateA" +
                    inst.getLocalName() + "-" + String.valueOf(i);
            Individual instanceDateTime =
                    ont.createIndividual(nsName + instance, classDateTime);
            try {
                String valueLOM =
                        LOMUtil.getString(LOMUtil.getAnnotationDate(lom, i).
                        value());
                if (valueLOM != null) {

                    RDFNode dateValue =
                            ont.createTypedLiteral(valueLOM);
                    instanceDateTime.setPropertyValue(dateTimeProperty,
                            dateValue);
                }
            } catch (Exception e) {
                String log =
                        "The DateTime field of the Annotation category must contain a value";
                errorWriter.println(log);
            }
            //8.3 description
            try {

                if (LOMUtil.getAnnotationDescription(lom, i) != null) {
                    instance =
                            "#lsAD" +
                            inst.getLocalName() + "-" + String.valueOf(i);

                    Individual langInstance =
                            ont.createIndividual(nsName +
                            instance, langStringClass);
                    int j =
                            0;
                    while (LOMUtil.getString(LOMUtil.getAnnotationDescription(lom,
                            i), j) != null) {
                        instance =
                                "#slsAD" +
                                inst.getLocalName() + "-" + String.valueOf(i);

                        Individual singleInstance =
                                ont.createIndividual(nsName + instance,
                                singleLangStringClass);
                        String descriptionLOM =
                                LOMUtil.getString(LOMUtil.getAnnotationDescription(lom,
                                i), j);
                        String languageLOM =
                                LOMUtil.getLanguage(LOMUtil.getAnnotationDescription(lom,
                                i), j);

                        if (descriptionLOM != null) {
                            RDFNode descriptionValue =
                                    ont.createTypedLiteral(descriptionLOM);
                            singleInstance.setPropertyValue(hasValueProperty,
                                    descriptionValue);
                        } else {
                            String log =
                                    "The Description field of the Annotation category must containt a value";
                            errorWriter.println(log);
                        }
                        if (languageLOM != null) {
                            RDFNode languageValue =
                                    ont.createTypedLiteral(languageLOM);
                            singleInstance.setPropertyValue(inLanguageProperty,
                                    languageValue);
                        } else {
                            String log =
                                    "The Language field of the Annotation category must contain a value";
                            errorWriter.println(log);
                        }
                        langInstance.addProperty(includeLangStringProperty,
                                singleInstance);
                        j++;
                    }
                    lomAnnotationInstance.setPropertyValue(annotationDescriptionProperty,
                            langInstance);
                    inst.setPropertyValue(annotationProperty,
                            lomAnnotationInstance);
                }
            } catch (Exception e) {
                String log =
                        "The Description field of the Annotation category must contain a value";
                errorWriter.println(log);
            }
            i++;

        }

    }
}
